<?php

class AssessmentTestCasesController extends AppController
{
  var $name = 'AssessmentTestCases';
  
  function add($assessment_id)
  {
    $this->set('assessment_id',$assessment_id);
    
    $conds = array('Assessment.id' => $assessment_id,
      'Assessment.user_id' => $this->Session->read('Auth.User.id'),
      'Assessment.submitted' => null);

    $assessment = $this->AssessmentTestCase->Assessment->find('first',array('conditions' => $conds, 'recursive' => 0));
    
    // User attempted to add to an invalid/unauthorized assessment.
    if (empty($assessment))
    {
      $this->redirect('/');
    }
    
    // Make sure this assessment is still "open".
    if (!empty($assessment['Assessment']['submitted']))
    {
      $this->redirect('/assessments/view/' . $assessment['Assessment']['id']);
    }
    
    if (!empty($this->data))
    {
      // In this case, the user defined a new test case. Save it to the db
      // before saving the specific assessment results.
      if (empty($this->data['AssessmentTestCase']['test_case_id']))
      {
        $this->data['TestCase']['user_id'] = $this->Session->read('Auth.User.id');
        
        if ($this->AssessmentTestCase->TestCase->save($this->data))
        {
          $this->data['AssessmentTestCase']['test_case_id'] = $this->AssessmentTestCase->TestCase->id;
        }
      }
      
      // Let the view know whether or not we have a valid test case id.
      $this->set('test_case_id',$this->data['AssessmentTestCase']['test_case_id']);

      $this->data['AssessmentTestCase']['assessment_id'] = $assessment_id;
      
      if ($this->AssessmentTestCase->save($this->data))
      {
        $this->flash('Test case added successfully.','/assessments/edit/' . $assessment_id);
      }
    }
    
    $test_cases_db = $this->AssessmentTestCase->TestCase->getForUserAssignment($this->Session->read('Auth.User.id'),$assessment['Submission']['assignment_id']);
    $test_cases = array();
    
    foreach ($test_cases_db as $tc)
    {
      $id = $tc['tc']['id'];
      $name = $tc['tc']['name'];
      $test_cases[$id] = $name;
    }

    $this->set('test_cases',$test_cases);
    $this->set('test_case_results',$this->AssessmentTestCase->TestCaseResult->find('list',array('order' => 'TestCaseResult.value')));
  }

  function delete($id,$assessment_id)
  {
    $conds = array('Assessment.id' => $assessment_id,
      'Assessment.user_id' => $this->Session->read("Auth.User.id"),
      'Assessment.submitted' => null);

    $assessment = $this->AssessmentTestCase->Assessment->find('first',array('conditions' => $conds, 'recursive' => 0));

    // User attempted to add to an invalid/unauthorized assessment.
    if (empty($assessment))
    {
      $this->redirect('/');
    }

    // Make sure this assessment is still "open".
    if (!empty($assessment[0]['a']['submitted']))
    {
      $this->redirect('/assessments/view/' . $assessment[0]['a']['id']);
    }

    $this->AssessmentTestCase->del($id);
    $this->flash('Test case results deleted successfully.','/assessments/edit/' . $assessment_id);
  }

  function edit($id)
  {
    $this->AssessmentTestCase->id = $id;

    if (!empty($this->data))
    {
      if ($this->AssessmentTestCase->save($this->data))
      {
        $this->flash('Your changes have been saved.','/assessments/edit/' . $this->data['AssessmentTestCase']['assessment_id']);
      }
    }
    else
    {
      $this->data = $this->AssessmentTestCase->read();

      // Make sure this assessment is still "open".
      if (!empty($this->data['Assessment']['submitted']))
      {
	$this->redirect('/assessments/view/' . $this->data['Assessment']['id']);
      }

      $test_cases = array($this->data['TestCase']['id'] => $this->data['TestCase']['name']);
    
      $this->set('test_cases',$test_cases);
      $this->set('test_case_results',$this->AssessmentTestCase->TestCaseResult->find('list',array('order' => 'TestCaseResult.value')));    
    }
  }
}
?>